package com.acme.bankapp.service.app;

import java.util.ArrayList;
import java.util.Collection;

public class EventDispatcher {

	class DispatchEntry {
		Class<? extends BankEvent> eventType;
		EventHandler handler;

		public DispatchEntry(Class<? extends BankEvent> eventType,
				EventHandler handler) {
			super();
			this.eventType = eventType;
			this.handler = handler;
		}
	}
	
	Collection<DispatchEntry> entries = new ArrayList<DispatchEntry>();

	public void addHandler(Class<? extends BankEvent> eventType, EventHandler handler) {
		entries.add(new DispatchEntry(eventType, handler));
	}
	
	public void dispatch(BankEvent event) {
		for (DispatchEntry de : entries) {
			if (de.eventType.isInstance(event)) {
				de.handler.handle(event);
			}
		}
	}
}
